<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Resources\CourseResource;
use App\Http\Resources\CourseResourceCollection;
use App\Models\Cates;
use Illuminate\Http\Request;
use App\Models\Courses as CourseModel;
use Illuminate\Support\Facades\DB;

class Courses extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('admin.course.index');
    }

    public function getCourses(Request $request)
    {
        $page = $request->get('page',10);
        $courses = CourseModel::all();
        $courses = new CourseResourceCollection($courses);
        $total = CourseModel::all()->count();
        return response()->json(['code'=>0,'data'=>$courses,'count'=>$total]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = Cates::orderBy("cate_sort",'asc')->get();
        return view('admin.course.add',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         $params = $request->except(['_token','fileselect','s']);
         $state = CourseModel::create($params);
         if($state){
             return response()->json(['status'=>200,'msg'=>'课程添加成功']);
         }else{
             return response()->json(['status'=>201,'msg'=>'课程添加失败']);
         }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
       $course = CourseModel::where(['id'=>$id])->first();
       $cates = Cates::all();

       return view('admin.course.edit',compact('course','cates'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->except(["_token",'fileselect','s']);
        $state = CourseModel::where(['id'=>$id])->update($data);
        if($state){
            return response()->json(['status'=>200,'msg'=>'课程修改成功']);
        }else{
            return response()->json(['status'=>201,'msg'=>'课程修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $state = CourseModel::where(['id'=>$id])->delete();
        if($state){
            return response()->json(['status'=>200,'msg'=>'删除课程成功']);
        }else{
            return response()->json(['status'=>201,'msg'=>'删除课程失败']);
        }
    }

    //添加课程章节
    public function catlog(Request $request,$courseId)
    {
        $chapter = DB::table('catlog')->where(['course_id'=>$courseId,'pid'=>'0'])->get(["id","catlog_title"]);
        return view('admin.course.catlog',compact('courseId','chapter'));
    }

    //保存课程章内容
    public function saveChapter(Request $request)
    {
        $params = $request->except(['s']);
        $stat = DB::table("catlog")->insertGetId($params);
        if($stat){
            return response()->json(['status'=>200,"msg"=>'添加课程章标题成功']);
        }else{
            return response()->json(['status'=>201,"msg"=>'添加课程章标题失败']);
        }
    }

    //添加课程章节内容
    public function saveVideo(Request $request)
    {
        $params = $request->except(['s']);
        $stat = DB::table('catlog')->insertGetId($params);
        if($stat){
            return response()->json(['status'=>200,"msg"=>'添加课程章节成功']);
        }else{
            return response()->json(['status'=>201,"msg"=>'添加课程章节失败']);
        }
    }

}
